package com.lc.railway.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.railway.R;
import com.lc.railway.http.request.NavListApi;
import com.lc.railway.http.response.NavListBean;
import com.lc.railway.manager.ActivityManager;
import com.lc.railway.other.DoubleClickHelper;
import com.lc.railway.other.IntentKey;
import com.lc.railway.ui.fragment.EducationFragment;
import com.lc.railway.ui.fragment.HomeFragment;
import com.lc.railway.ui.fragment.MeFragment;
import com.lc.railway.ui.fragment.PartyHistoryFragment;
import com.lc.railway.ui.fragment.TrainFragment;
import com.lc.railway.util.SPUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 首页界面
 */
public final class HomeActivity extends WaterActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    Fragment fragment = null;
    RadioGroup rgHomePageBottom;
//    @BindView(R.id.wmv)
//    WaterMarkView wmv;
    private List<NavListBean.DataBean> dataBeanList = new ArrayList<>();
    private RadioButton radioButton1, radioButton2, radioButton3, radioButton4;
    private ImageButton imageButton;
    private List<Fragment> mFragmentList;
    private FragmentTransaction mFragmentTransaction;

    @Override
    protected int getLayoutId() {
        return R.layout.home_activity;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onStart() {
        super.onStart();
//        wmv.setParams(SPUtil.get(getContext(), IntentKey.NAME,"")+SPUtil.get(getContext(), IntentKey.PHONE,""));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        nav_list();
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new PartyHistoryFragment());
        mFragmentList.add(new EducationFragment());
        mFragmentList.add(new TrainFragment());
        mFragmentList.add(new MeFragment());
        mFragmentList.add(new HomeFragment());
        radioButton1 = findViewById(R.id.rb_home);
        radioButton2 = findViewById(R.id.rb_buy);
        radioButton4 = findViewById(R.id.rb_my);
        radioButton3 = findViewById(R.id.rb_find);
        rgHomePageBottom = findViewById(R.id.rg_home_page_bottom);
        rgHomePageBottom.setOnCheckedChangeListener(this);
        switchContent(mFragmentList.get(4));


//
    }

    private void nav_list() {
        EasyHttp.post(this)
                .api(new NavListApi())
                .request(new HttpCallback<NavListBean>(this) {
                    @Override
                    public void onSucceed(NavListBean data) {
                        SPUtil.remove(HomeActivity.this, IntentKey.TEXT);
                        dataBeanList.clear();
                        dataBeanList.add(new NavListBean.DataBean(0, "首页", 0, false));

                        dataBeanList.addAll(data.getData());
                        dataBeanList.add(new NavListBean.DataBean(0, "电子邮箱", 0, false));

                        dataBeanList.add(new NavListBean.DataBean(0, "警营风采", 0, false));
                        dataBeanList.add(new NavListBean.DataBean(0, "荣誉榜", 0, false));
                        dataBeanList.add(new NavListBean.DataBean(0, "局长邮箱", 0, false));
                        dataBeanList.add(new NavListBean.DataBean(0, "政委邮箱", 0, false));
                        dataBeanList.add(new NavListBean.DataBean(0, "值班列表", 0, false));
                        dataBeanList.add(new NavListBean.DataBean(0, "发布榜单", 0, false));
                        dataBeanList.add(new NavListBean.DataBean(0, "视频", 0, false));
                        dataBeanList.add(new NavListBean.DataBean(0, "友情链接", 0, false));
                        dataBeanList.add(new NavListBean.DataBean(0, "通讯录", 0, false));

                        Gson g = new Gson();
                        String jsonString = g.toJson(dataBeanList);
                        Log.e("TAG", "onSucceed: " + jsonString);

                        SPUtil.put(HomeActivity.this, IntentKey.TEXT, jsonString);
                    }
                });
    }

    @Override
    public void onBackPressed() {
        if (!DoubleClickHelper.isOnDoubleClick()) {
            toast(R.string.home_exit_hint);
            return;
        }

        // 移动到上一个任务栈，避免侧滑引起的不良反应
        moveTaskToBack(false);
        postDelayed(() -> {
            // 进行内存优化，销毁掉所有的界面
            ActivityManager.getInstance().finishAllActivities();
            // 销毁进程（注意：调用此 API 可能导致当前 Activity onDestroy 方法无法正常回调）
            // System.exit(0);
        }, 300);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                switchContent(mFragmentList.get(0));
                break;
            case R.id.rb_buy:
                switchContent(mFragmentList.get(1));
                break;
            case R.id.rb_find:
                switchContent(mFragmentList.get(2));
                break;
            case R.id.rb_my:
                switchContent(mFragmentList.get(3));
                break;
        }
    }

    private Fragment mContent = new Fragment();

    private void switchContent(Fragment to) {
        Log.e("addFragment", "### " + to.getId() + "  " + to.isAdded() + "  " + to.isHidden());
        if (mContent != to) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (!to.isAdded()) {
                transaction.hide(mContent).add(R.id.fg_main, to).commitAllowingStateLoss();
            } else {
                transaction.hide(mContent).show(to).commitAllowingStateLoss();
            }
            mContent = to;
        }
    }


    @OnClick(R.id.ib_mall)
    public void onClick() {


        rgHomePageBottom.clearCheck();
        switchContent(mFragmentList.get(4));

    }
}